unit GLGUIMultiSkin;

interface

uses
  GLSCrossXML,
  GLSGenerics,
  GLGUISkin,
  GLGUISkinManager,
  GLGUIUtils,
  GLGUIExceptions;

type
  TGLGUIMultiSkinElement = class
  public
    FSkin: String;
    FName: String;
  end;

  TGLGUIMultiSkinElementsList = GList<TGLGUIMultiSkinElement>;

  TGLGUIMultiSkin = class(TGLGUICustomSkin)
  private
    FSkins: TGLGUIMultiSkinElementsList;
  public
    constructor Create(); override;
    procedure Parse; override;
    function SkinByName(AName: String): String;
    destructor Destroy(); override;
  end;

implementation

constructor TGLGUIMultiSkin.Create();
begin
  inherited;
  FSkins := TGLGUIMultiSkinElementsList.Create;
end;

procedure TGLGUIMultiSkin.Parse;
var
  i: Integer;
  vCurentSkinElement: GLSXMLNode;
  vCurrentSkinElement: TGLGUIMultiSkinElement;
  vCurrentAttribute: String;
begin
  if FXMLNode = nil then
    Exit;

  for i := 0 to FXMLNode.ChildNodes.Count - 1 do
  begin
    vCurentSkinElement := FXMLNode.ChildNodes.Get(i);
    if vCurentSkinElement.LocalName = 'SkinElement' then
    begin
      vCurrentSkinElement := TGLGUIMultiSkinElement.Create;
      GetXMLAttribute(vCurentSkinElement, 'Name', vCurrentAttribute);
      vCurrentSkinElement.FName := vCurrentAttribute;
      GetXMLAttribute(vCurentSkinElement, 'Skin', vCurrentAttribute);
      vCurrentSkinElement.FSkin := vCurrentAttribute;
      FSkins.Add(vCurrentSkinElement);
    end;
  end;

  FIsParsed := True;
end;

function TGLGUIMultiSkin.SkinByName(AName: String): String;
var
  i: Integer;
begin
  Result := '';
  for i := 0 to FSkins.Count-1 do
  begin
    if FSkins.Items[i].FName = AName then
    begin
      Result := FSkins.Items[i].FSkin;
      Exit;
    end;
  end;
  if SameStr(Result, '') then
    raise EGLGUIException.Create(ESkinNotFoundInCollection(AName, Name));
end;

destructor TGLGUIMultiSkin.Destroy();
var
  i: Integer;
begin
  for i := 0 to FSkins.Count - 1 do
    FSkins[i].Free;
  FSkins.Free;
 
  inherited;
end;

initialization
  RegisterSkin(TGLGUIMultiSkin);

finalization

end.
